package i2.application.fmw.cerbere.selenium.pages;

import i2.application.fmw.selenium.exceptions.WrongPageException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.selenium.Wait;
import com.thoughtworks.selenium.Wait.WaitTimedOutException;

public class AccueilApplicationPage {

	private static final Logger LOGGER = LoggerFactory.getLogger(AccueilApplicationPage.class);
	
	protected WebDriver driver;
	
//	@FindBy(xpath="//a[@href='deconnexion.action']")
	@FindBy(xpath="//a[text()='Deconnexion']")
	private WebElement deconnection;	

	
	public AccueilApplicationPage(WebDriver driver) {
		this.driver = driver;
		PageFactory.initElements(driver, this);
		Wait wait =  new Wait() {
			@Override
			public boolean until() {
				LOGGER.debug("until");
				return (getTitle().equals("Trafic2000")||getTitle().equals("Identification pour dossier de crise"));
			}
			
		};
		
		LOGGER.debug("before wait");
		try {
			wait.wait("Accueil not found", 5000);
		} catch (WaitTimedOutException e) {
			throw new WrongPageException();
		}
	}
	
	public void open(String url) {
		driver.get(url);
	}

	public void close() {
		driver.quit();
	}
	
	public String getTitle() {
		return driver.getTitle();
	}

	public void deconnecter() {
		LOGGER.debug("début deconnecter()");
		deconnection.click();
	}

}